package com.kuang.sync;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.kuang.mapper.AstCloseLogMapper;
import com.kuang.mapper.AstOpenLogMapper;
import com.kuang.mapper.AstRunLogMapper;
import com.kuang.pojo.AstCloseLog;
import com.kuang.pojo.AstOpenLog;
import com.kuang.pojo.AstRunLog;
import org.apache.commons.collections4.MapUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@Service
public class MqttSyncServiceImpl implements MqttSyncService {
    @Resource
    AstRunLogMapper astRunLogMapper;
    @Resource
    AstOpenLogMapper astOpenLogMapper;
    @Resource
    AstCloseLogMapper astCloseLogMapper;
    @Resource
    ExternalApiService externalApiService;

//    private  static MqttSyncServiceImpl mqttSyncService;
//    @PostConstruct
//    public void init() {
//        mqttSyncService = this;
//    }

    //5.运行记录  wd/run
    @Override
    public String wdDeviceRunSrv(String json) {
        JSONObject preant = JSON.parseObject(json);
        String cmd = (String) preant.get("cmd");
        JSONObject jsonObject = preant.getJSONObject("data");

        AstRunLog astRunLog = new AstRunLog();
        //存储cmd
        astRunLog.setCmd(cmd);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        //主键
        astRunLog.setAst_run_log_id(String.valueOf(UUID.randomUUID()));
        astRunLog.setR_type(MapUtils.getString(jsonObject, "rType", ""));
        astRunLog.setR_status(MapUtils.getString(jsonObject, "rStatus", ""));
        astRunLog.setR_network(MapUtils.getString(jsonObject, "rNetwork", ""));
        astRunLog.setR_code(MapUtils.getString(jsonObject, "rCode", ""));
        astRunLog.setR_dev_name(MapUtils.getString(jsonObject, "rDevName", ""));
        astRunLog.setR_dev_id(MapUtils.getString(jsonObject, "rDevId", ""));
        String rTime = MapUtils.getString(jsonObject, "rTime", "");
        try {
            Date parse = simpleDateFormat.parse(rTime);
            astRunLog.setR_time(parse);
        } catch (ParseException e) {
            e.printStackTrace();
            return "数据传输格式错误！";
        }
        astRunLog.setLat(MapUtils.getString(jsonObject, "lat", ""));
        astRunLog.setLon(MapUtils.getString(jsonObject, "lon", ""));
        //int insert =mqttSyncService.astRunLogMapper.insert(astRunLog);
        int insert = astRunLogMapper.insert(astRunLog);

        return null;
    }

    //5.2开泵  wd/open
    @Override
    public String wdDeviceOpenSrv(String json) {
        JSONObject preant = JSON.parseObject(json);
        String cmd = (String) preant.get("cmd");
        JSONObject jsonObject = preant.getJSONObject("data");

        AstOpenLog astOpenLog = new AstOpenLog();
        //存储cmd
        astOpenLog.setCmd(cmd);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        //主键
        astOpenLog.setAst_open_log_id(String.valueOf(UUID.randomUUID()));
        astOpenLog.setR_dev_id(MapUtils.getString(jsonObject, "cDevId", ""));
        astOpenLog.setR_code(MapUtils.getString(jsonObject, "cCode", ""));
        astOpenLog.setCard_type(MapUtils.getString(jsonObject, "cardType", ""));
        astOpenLog.setCard_no(MapUtils.getString(jsonObject, "cardNo", ""));

        String rTime = MapUtils.getString(jsonObject, "cOpenTime", "");
        try {
            Date parse = simpleDateFormat.parse(rTime);
            astOpenLog.setC_open_time(parse);
        } catch (ParseException e) {
            e.printStackTrace();
            return "数据传输格式错误！";
        }

        Double cMoney = MapUtils.getDouble(jsonObject, "cMoney", 0.0);
        astOpenLog.setC_money(new BigDecimal(cMoney));

        int insert = astOpenLogMapper.insert(astOpenLog);

        return null;
    }

    //5.3关泵  wd/close
    @Override
    public String wdDeviceCloseSrv(String json) {
        JSONObject preant = JSON.parseObject(json);
        String cmd = (String) preant.get("cmd");
        JSONObject jsonObject = preant.getJSONObject("data");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

        AstCloseLog astCloseLog = new AstCloseLog();
        //存储cmd
        astCloseLog.setCmd(cmd);
        //主键
        astCloseLog.setAst_close_log_id(String.valueOf(UUID.randomUUID()));
        astCloseLog.setR_dev_id(MapUtils.getString(jsonObject, "cDevId", ""));
        astCloseLog.setR_code(MapUtils.getString(jsonObject, "cCode", ""));
        astCloseLog.setCard_type(MapUtils.getString(jsonObject, "cardType", ""));
        astCloseLog.setCard_no(MapUtils.getString(jsonObject, "cardNo", ""));

        String rTime = MapUtils.getString(jsonObject, "closeTime", "");
        try {
            Date parse = simpleDateFormat.parse(rTime);
            astCloseLog.setClose_time(parse);
        } catch (ParseException e) {
            e.printStackTrace();
            return "数据传输格式错误！";
        }

        Double totalMoney = MapUtils.getDouble(jsonObject, "totalMoney", 0.0);
        astCloseLog.setTotal_money(new BigDecimal(totalMoney));

        Double fee = MapUtils.getDouble(jsonObject, "fee", 0.0);
        astCloseLog.setFee(new BigDecimal(fee));

        Double money = MapUtils.getDouble(jsonObject, "money", 0.0);
        astCloseLog.setC_money(new BigDecimal(money));

        Double uele = MapUtils.getDouble(jsonObject, "uele", 0.0);
        astCloseLog.setUele(new BigDecimal(uele));

        Double uelePrice = MapUtils.getDouble(jsonObject, "uelePrice", 0.0);
        astCloseLog.setUele_price(new BigDecimal(uelePrice));

        Double uwater = MapUtils.getDouble(jsonObject, "uwater", 0.0);
        astCloseLog.setUwater(new BigDecimal(uwater));

        Double uwaterPrice = MapUtils.getDouble(jsonObject, "uwaterPrice", 0.0);
        astCloseLog.setUwater_price(new BigDecimal(uwaterPrice));

        Double utime = MapUtils.getDouble(jsonObject, "utime", 0.0);
        astCloseLog.setUtime(new BigDecimal(utime));

        int insert = astCloseLogMapper.insert(astCloseLog);

        return null;
    }

    //5.4实体卡充值  pay/entityAdd--直接发送给PC端
    @Override
    public String payEntityAddSrc(String json) {
        String s = externalApiService.putPayEntityAdd(json);
        return s;
    }

    @Override
    public String controllerHeartSrv(String json) {
        return null;
    }


}
